package com.manage.usermanage.security.service;

import com.manage.usermanage.common.utils.StringUtils;
import com.manage.usermanage.dao.ds2.UserMapper2;
import com.manage.usermanage.domain.entity.User;
import com.manage.usermanage.domain.model.LoginUser;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.Collections;

public class LoginUserService implements UserDetailsService {

    @Resource
    private UserMapper2 subUserMapper; // 用户数据库

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = subUserMapper.getUserByName(username);
        if (StringUtils.isNull(user)) {
            throw new RuntimeException("用户不存在");
        }

        LoginUser loginUser = new LoginUser();
        loginUser.setUserId(user.getId());
        loginUser.setPasswd(user.getPasswd());
        loginUser.setSalt(user.getSalt());
        loginUser.setNickName(user.getNickName());
        return loginUser;
    }
}
